package at.fhj.itm10.ticketmaster.beans;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;

import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.dao.DAOFactory;
import at.fhj.itm10.ticketmaster.dao.EventDAO;
import at.fhj.itm10.ticketmaster.dao.impl.DAOFactoryImpl;
import at.fhj.itm10.ticketmaster.entities.Event;
import at.fhj.itm10.ticketmaster.services.EventService;
import at.fhj.itm10.ticketmaster.services.ServiceException;
import at.fhj.itm10.ticketmaster.services.ServiceFactory;

/**
 * AnimationBean.java
 * 
 * @author Katharina Haidinger
 * @author Marvin Puchmüller
 */
public class AnimationBean {

	private EventDAO dao;
	private List<Event> eventlist = new ArrayList<Event>();
	private EventService service;
	private Logger logger = Logger.getLogger(AnimationBean.class);
	@PostConstruct
	public void init() {
    	try {
			DAOFactory daoFactoryImpl;
			daoFactoryImpl = new DAOFactoryImpl();
			ServiceFactory sf = new ServiceFactory();
			EventService service = sf.getEventService();
			setDao(daoFactoryImpl.getEventDAO());
			eventlist = service.findTopEvents();
			eventlist.addAll(service.findRandomEvents());
		}
    	catch (DAOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Gets random events
	 * 
	 * @return List<Event>
	 * @exception DAOException
	 * 
	 */
	
	public void getTopEvents() {
		try {
			eventlist.addAll(service.findTopEvents());	
			} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Searching for similar events via eventtype-id 
	 * 
	 * @param id
	 * @return List<Event>
	 * @exception DAOException
	 * @exception SQLExceptions
	 * 
	 */
	
	public void getSameEvents(long id) {
		try {
			eventlist.addAll(service.findSameEvents(id));	
			} catch (DAOException e) {
			logger.error(e);
		} catch (SQLException e) {
				logger.error(e);
			}
	}
	
	public long getEventId(int id) {
		return eventlist.get(id).getId();
	}
	
	public String getHeadline(int id) {
		return eventlist.get(id).getName();
	}
	
	public String getDate(int id) {
		String[] splitdate = eventlist.get(id).getBegin().split(":");
		return splitdate[0]+":"+splitdate[1];
	}
	
	public String getDescription(int id) {
		return eventlist.get(id).getDescription();
	}
	
	public String getFlyer(int id) {
		String flyer = "img/dummy/400x300.gif";
		if(eventlist.get(id).getFlyer() == null)
			eventlist.get(id).setFlyer(flyer);
		else eventlist.get(id).setFlyer(eventlist.get(id).getFlyer());
			return eventlist.get(id).getFlyer();
	}
	
	public void getRandomEvents() {
		try {
			eventlist.addAll(service.findRandomEvents());	
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public EventDAO getDao() {
		return dao;
	}

	public void setDao(EventDAO dao) {
		this.dao = dao;
	}		
}
